#include "esp8266_device.h"
#include "atcommand.h"
#include "driver_uart.h"
#include <stdio.h>
#include <string.h>

static char infobuf[INFOSIZE];

static int ESP8266Init(struct netdevice *pDev)
{
   USART3_RingbufferInit();   
   ATCommandSend("AT\r\n", infobuf, iTimeout);
   return  ATCommandSend("AT+CWMODE=3\r\n", infobuf, iTimeout);   
}


static int ESP8266Connect(struct netdevice *pDev)
{
    char cmd[100] = {0};	
	sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"\r\n", SSID, PASSWORD);
    return ATCommandSend(cmd, infobuf, iTimeout*10);
}


static int ESP8266Transfer(struct netdevice *pDev)
{
    char cmd[100] = {0};	 
    sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%s,%s,2\r\n", REMOTEIP, REMOTEPORT, ESP8266PORT);
    return ATCommandSend(cmd, infobuf, iTimeout);
}

  
static int ESP8266GetInfo(struct netdevice *pDev, char *esp8266StaIp)
{
    int flag = PDFAIL;
    char *tmp = NULL;
    int i = 0;
    flag = ATCommandSend("AT+CIFSR\r\n", infobuf, iTimeout*5);
    if(flag == PDSUCCESS)
    {
        tmp = strstr(infobuf, "+CIFSR:STAIP");
        if(tmp != NULL)
        {
            tmp += strlen("+CIFSR:STAIP,\"");
			for(i = 0; tmp[i] != '\"'; i++)
			{
				esp8266StaIp[i] = tmp[i];
			}
			esp8266StaIp[i] = '\0';
//            memcpy(esp8266StaIp, pDev->ipaddr, i);
			return PDSUCCESS; 
        }
    }
    return PDFAIL;
}


static int ESP8266Close(struct netdevice *pDev)
{
     return ATCommandSend("AT+CIPCLOSE\r\n", infobuf, iTimeout);  
}


static int ESP8266Recv(struct netdevice *pDev, char *data, int *pLen, int iTimeoutMS)
{
  return ATDataRecv(data, pLen, iTimeoutMS);
}


static int ESP8266GetNetTime(struct netdevice *pDev, pNetTime time)
{
    char *tmp = NULL;
    static int iFlag = PDFAIL;
 
    while(1)
    {
       if(iFlag == PDFAIL)
       {
          iFlag = ATCommandSend("AT+CIPSNTPCFG=1,8\r\n", infobuf, iTimeout*5);  
       }
       if(iFlag == PDSUCCESS)
       {
           ATCommandSend("AT+CIPSNTPTIME?\r\n", infobuf, iTimeout*5);
           tmp = strstr(infobuf, "+CIPSNTPTIME:");
           if(tmp != NULL)
           {
                tmp = tmp + strlen("+CIPSNTPTIME:");
                memcpy(time->week, tmp, 3);
                time->week[3] = '\0';
            
               
                tmp = tmp + 4;
                memcpy(time->month, tmp, 3);
                time->month[3] = '\0';
               
               
                tmp = tmp + 4;
                memcpy(time->day, tmp, 2);
                time->day[2] = '\0'; 

               
                tmp = tmp + 3;
                memcpy(time->hour, tmp, 2);
                time->hour[2] = '\0'; 
               
               
                tmp = tmp + 3;
                memcpy(time->min, tmp, 2);
                time->min[2] = '\0'; 
           
               
                tmp = tmp + 3;
                memcpy(time->sec, tmp, 2);
                time->sec[2] = '\0';
              
                
                tmp = tmp + 3;
                memcpy(time->year, tmp, 4);
                time->year[4] = '\0';  
                if(strstr(time->year, "1970") != NULL)
                {
                    iFlag  = PDFAIL;
                    continue;
                } 
                return PDSUCCESS;
           } 
       }
    }
}


static netDevice NetDevices = {
    .name = "ESP8266",
    .ipaddr = "\0",
    .Init = ESP8266Init,
    .Connect = ESP8266Connect,
    .GetInfo = ESP8266GetInfo,
    .Close = ESP8266Close,
    .Transfer = ESP8266Transfer,  
    .GetNetTime = ESP8266GetNetTime,
    .Recv = ESP8266Recv
};


pNetDevice GetNetDev(char *name)
{
    if(strcmp(name, "ESP8266") == 0)
    {
        return &NetDevices;
    }
    return NULL;
}

